<?php
/*
 * File: UserController.php
 * Project: NewDemo
 * Created Time: 2024-05-29 11:27:52 am
 * Author: WangYuQiang && WangpeiGang (719404171@qq.com)
 * -----
 * Last Modified: 2024-07-25 2:30:35 pm
 * Modified By: WangYuQiang (719404171@qq.com>)
 * -----
 * Copyright (c) 2023 - 2024 成都睿創科技有限公司
 */

namespace App\Http\Controllers\Api;

use App\Models\User;
use Illuminate\Http\Request;
use Tests\CreatesApplication;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Api\BaseController;

class UserController extends Controller{
    
    private $Users;
    public function __construct()
    {
        $this->Users = new User();
    }

    /**
     * @param Request $request
     * @return array
     * 獲取個人信息
     */
    public function getUsersInfo(Request $request){
        $user = auth('api')->user();
        $user_id=$user->user_id;
        if (empty($user_id)) {
            return $this->success(__('api.user_notexist'));
        }
        $results = $this->Users->getUsersInfo($user_id);
        return $results;

    }


    


    /**
     * @param Request $request
     * @return array
     * 忘記/修改密碼
     */
    public function forgetPass(Request $request){
        if (!$request->has("user_phone")) {
            return $this->error(__('api.phone_missing'));
        } else if (!$request->has("user_password")) {
            return $this->error(__('api.password_missing'));
        } else {
            DB::beginTransaction();
            try {
                $data = User::where('user_phone',$request->post("user_phone"))->where('area_code',$request->post('area_code'))->first();
                if(!empty($data)){
                    $password = bcrypt($request->post('user_password'));
                    $user = User::where('user_phone', $request->post('user_phone'))->where('area_code',$request->post('area_code'))->update(['user_password' => $password]);
                    if ($user) {
                        DB::commit();
                        return $this->success(__('api.edit_success'));
                    } else {
                        DB::rollBack();
                        return $this->error(__('api.edit_fail'));
                    }
                }else{
                    DB::rollBack();
                    return $this->error(__('api.phone_notexist'));
                }
            }catch (\Exception $e) {
                DB::rollBack();
                return $this->error( __('web.edit_fail') , $e->getMessage());
            }
        }
    }



    /**
     * @param Request $request
     * @return array
     * 修改用戶信息
     */
    public function UpdateUsers(Request $request){
        if(empty($request->post())){
            return $this->parametersError(__('api.param_error'));
        }else{
            $user = auth('api')->user();
            $user_id=$user->user_id;
            $data = $request->post();
            unset($data['user_integral']);
            unset($data['user_integral_all']);
            unset($data['user_balance']);
            $results = $this->Users->UpdateUsers($user_id , $data);
            return $results;
        }
    }


    /**
     * @param Request $request
     * @return array
     * 修改綁定手機號
     */
    public function UpdatePhone(Request $request){
        if(empty($request->post('user_phone'))){
            return $this->parametersError(__('api.param_error'));
        }else{
            $user = auth('api')->user();
            $user_id=$user->user_id;
            $data = $request->post();
            $ExistPhone = DB::table('users')->where('user_phone',$data['user_phone'])->where('area_code',$data['area_code'])->where('user_status',1)->first();
            if(!empty($ExistPhone)){
                return $this->error(__('api.phone_exist'));
            }else{
                $results = $this->Users->UpdatePhone($user_id , $data);
                return $results;
            }
        }
    }

    
    
    /**
     * @param Request $request
     * @return array
     * 密碼驗證
     */
    public function PasswordVerify(Request $request){
        if (!$request->has("user_phone")) {
            return $this->error(__('api.phone_missing'));
        } else if (!$request->has("user_password")) {
            return $this->error(__('api.password_missing'));
        } else {
            // 传递给attempt的数组只是一个数组；它与您的数据库表无关。您不必使用'user_phone'和'user_password'。
            if (Auth::attempt(['user_phone' => request('user_phone'), 'password' =>request('user_password') ,'area_code' => request('area_code')])) {
                return $this->success(__('api.verify_success'));
            } else {
                return $this->error(__('api.verify_fail'));
            }
        }
    }


    /**
     * @param Request $request
     * @return array
     * 删除賬戶
     */
    public function deleteUser(Request $request){
        $user = auth('api')->user();
        $data['user_id']=$user->user_id;
        DB::beginTransaction();
        try {
            $results = DB::table('users')->where('user_id',$data['user_id'])->delete();
            if($results){
                DB::commit();
                return $this->success(__('api.delete_success'));
            }else{
                DB::rollBack();
                return $this->error(__('api.delete_fail'));
            }
        }catch (\Exception $e) {
            DB::rollBack();
            return $this->error( __('api.delete_fail') , $e->getMessage());
        }
    }





    
    /**
     * @param Request $request
     * @return array
     * 註銷賬戶
     */
    public function BannedUser(Request $request){
        $username = $request->post('username');
        $password = $request->post('password');
        if(empty($request->post('areaCode'))){
            if (Auth::attempt(['email' => request('username'), 'password' => request('password')])) {
                $userinfo = Auth::user();
            }else{
                return $this->error(__('api.login_error'));
            }
        }else{
            if (Auth::attempt(['phone' => request('username'), 'password' => request('password')])) {
                $userinfo = Auth::user();
            }else{
                return $this->error(__('api.login_error'));
            }
        }
        DB::beginTransaction();
        try {
            if(!empty($userinfo)){
                $datas = DB::table('users')->where('id',$userinfo['id'])->delete();
                if($datas){
                    DB::commit();
                    return $this->success(__('api.delete_success'));
                }else{
                    DB::rollBack();
                    return $this->error(__('api.delete_fail'));
                }
            }else{
                return $this->error(__('api.delete_fail'));
            }
        }catch (\Exception $e) {
            DB::rollBack();
            return $this->error( __('api.delete_fail') , $e->getMessage());
        }
    }


/**********************************************************END**********************************************************************/
}
